/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package storymotion.access;

import storymotion.dao.DAO;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class AccessDAO extends DAO {

    public AccessDAO() throws SQLException {

    }

    /**
     *
     * @param login
     * @param password
     * @return
     * @throws SQLException
     */
    public boolean loginValid(String login, String password) throws SQLException {
        if (login == null || password == null || login.trim().length() == 0 || password.trim().length() == 0) {
            return false;
        }
        boolean result = false;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT login, password FROM \"user\" WHERE login = ?");
            preparedStatement.setString(1, login);
            ResultSet resultSet = preparedStatement.executeQuery();
            String recoveryPassword = "";
            while (resultSet != null && resultSet.next()) {
                recoveryPassword = resultSet.getString("password");
            }
            resultSet.close();
            preparedStatement.close();
            if (recoveryPassword.equalsIgnoreCase(password)) {
                result = true;
            }
        } catch (SQLException ex) {
            throw ex;
        }
        return result;
    }

}
